package aar.test.tessocr;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import aar.test.tessocr.adapter.CabDetailAdapter;
import aar.test.tessocr.baseApplication.BaseApplication;
import aar.test.tessocr.model.CabDetailModel;
import aar.test.tessocr.model.CabinetModel;
import aar.test.tessocr.model.DialogCallback;
import aar.test.tessocr.model.LzyResponse;
import aar.test.tessocr.tessOcrUtil.MyGridView;
import aar.test.tessocr.utils.Urls;
import butterknife.BindView;
import butterknife.ButterKnife;
import clx.tessocr.ocreng.R;
import lib_zxing.activity.CodeUtils;
import okgo.OkGo;
import okgo.model.Response;
import okhttp3.RequestBody;

import static aar.test.tessocr.Login1Activity.JSON;


public class CabinetDetailsActivity extends AppCompatActivity {


    @BindView(R.id.title)
    TextView title;
    private RadioGroup gridRadioGroup;

    private Button open_grid;

    private TextView choiceCab_spinner;
    private TextView choiceCab_spinner2;


    private List<CabinetModel> cabList;

    private List<String> data_list = new ArrayList<>();

    private int selectId = 0;

    private int cabType = 1;

    private ArrayAdapter<String> cabAdater;

    private Context mContext;

    private DisplayMetrics dm;

    private boolean isPerss = false;

    private Toolbar toolbar;

    private MyGridView cab_grid_view;

    private LinearLayout left_layout;

    private Button cab_open_all;
    private Button edit_cabinet;
    private String cabinetName = "";
    private String cabinetId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chioce_cab);
        ButterKnife.bind(this);
        mContext = this;

        cabinetName = getIntent().getStringExtra("cabinetName");
        cabinetId = getIntent().getStringExtra("cabinetId");
        toolbar = (Toolbar) findViewById(R.id.toolbar_normal);
        setSupportActionBar(toolbar);
        initView();

        dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        companyCabDetail();
    }


    private void initView() {
        title.setText(cabinetName);

        edit_cabinet = findViewById(R.id.edit_cabinet);

        edit_cabinet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                Intent intent = null;
                intent = new Intent(mContext, EditCabintActivity.class);
                intent.putExtra("cabinetId", cabinetId);
                startActivityForResult(intent, 100);
            }
        });


        cab_open_all = findViewById(R.id.cab_open_all);
        cab_open_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //openAllCab();

            }
        });


        cab_grid_view = findViewById(R.id.cab_grid_view);

        cab_grid_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                if (null != mList && mList.size() > 0) {

                    BaseApplication.getIntance().setmCabDetailModel(mList.get(position));
//
                }


            }
        });

        left_layout = findViewById(R.id.left_layout);
        left_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private List<CabDetailModel> mList;

    private void companyCabDetail() {
        /*if (null == BaseApplication.getIntance().getCabinetListMpdel()) {
            Toast.makeText(mContext, "没有柜子信息", Toast.LENGTH_LONG).show();
            return;
        }

        if (null == BaseApplication.getIntance().getCabinetListMpdel().getCabinetid()) {
            Toast.makeText(mContext, "没有柜子ID信息", Toast.LENGTH_LONG).show();
            return;
        }*/

        Map<String, Object> map = new HashMap<>();
        map.put("cabinetId", cabinetId);

        Gson gg = new Gson();
        String json = gg.toJson(map);

        RequestBody body = RequestBody.create(JSON, json);
        OkGo.<LzyResponse<List<CabDetailModel>>>post(Urls.URL_CABINETDETAIL)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse<List<CabDetailModel>>>(CabinetDetailsActivity.this) {
                    @Override
                    public void onSuccess(Response<LzyResponse<List<CabDetailModel>>> response) {


                        Log.e("MainActivity", "onSuccess-----logout---------------");

                        if (null != response.body().data && response.body().data.size() != 0) {
                            mList = new ArrayList<>();
                            mList.addAll(response.body().data);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {

                                    cab_grid_view.setVisibility(View.VISIBLE);
                                    CabDetailAdapter adapter = new CabDetailAdapter(mContext, mList);
                                    cab_grid_view.setAdapter(adapter);
                                }
                            });
                        } else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    cab_grid_view.setVisibility(View.GONE);
                                    Toast.makeText(mContext, "没有柜子信息", Toast.LENGTH_SHORT).show();
                                }
                            });


                        }


                    }

                    @Override
                    public void onError(Response<LzyResponse<List<CabDetailModel>>> response) {
                        Log.e("MainActivity", "onError---------------------");
                    }
                });


    }


    private void openAllCab() {

        if (null == BaseApplication.getIntance().getCabinetListMpdel()) {
            Toast.makeText(mContext, "没有柜子信息", Toast.LENGTH_SHORT).show();
            return;
        }

        if (null == BaseApplication.getIntance().getCabinetListMpdel().getCabinetid()) {
            Toast.makeText(mContext, "没有柜子ID信息", Toast.LENGTH_SHORT).show();
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("cabinetId", BaseApplication.getIntance().getCabinetListMpdel().getCabinetid());
        map.put("type", 1);

        Gson gg = new Gson();
        String json = gg.toJson(map);

        RequestBody body = RequestBody.create(JSON, json);
        OkGo.<LzyResponse>post(Urls.URL_ADMINOPENGRID)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse>(CabinetDetailsActivity.this) {
                    @Override
                    public void onSuccess(Response<LzyResponse> response) {


                        Log.e("MainActivity", "onSuccess-----logout---------------" + response.body().data);


                        if (null != response.body().msg) {
                            Toast.makeText(mContext, response.body().msg, Toast.LENGTH_SHORT).show();
                        }


                    }

                    @Override
                    public void onError(Response<LzyResponse> response) {
                        Log.e("MainActivity", "onError---------------------");
                        if (null != response.body().msg) {
                            Toast.makeText(mContext, response.body().msg, Toast.LENGTH_SHORT).show();
                        }
                    }
                });


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        if (resultCode == RESULT_OK) {

            if (null != data) {

                String mm = data.getStringExtra("cabinetName");

                if (null != mm) {

                    title.setText(mm);
                }
            }
        }
    }
}
